package yixaun.tim.service.impl;


import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import yixaun.tim.bean.chat.*;
import yixaun.tim.common.TimException;
import yixaun.tim.service.TimChatService;
import yixaun.tim.service.TimService;
import yixaun.tim.util.JsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pengshiqing
 * @date 2019-07-20
 * @Description: 单聊消息 https://cloud.tencent.com/document/product/269/1610
 */

@Slf4j
public class TimChatServiceImpl implements TimChatService {


    private TimService timService;

    public TimChatServiceImpl(TimService timService) {
        this.timService = timService;
    }






    @Override
    public void sendTextMsg(@NonNull String fromAccount, @NonNull String toAccount, @NonNull List<String> msgList, ChatMsgEnum chatMsgEnum) throws TimException {
        String api = "v4/openim/sendmsg";

        if( chatMsgEnum == null ){
            chatMsgEnum = ChatMsgEnum.NoSync ;
        }

        Map<String, Object> body = new HashMap<>(4);
        body.put("SyncOtherMachine", chatMsgEnum.getType());
        body.put("From_Account",fromAccount);
        body.put("To_Account",toAccount);
        body.put("MsgRandom", RandomUtils.nextInt(10000000,100000000));

        List<MsgBody> msgBodies = new ArrayList<>();

        for (String msg : msgList) {
            MsgBody msgBody = new MsgBody();
            msgBody.setMsgType("TIMTextElem");
            msgBody.setMsgContent(new MsgContent(msg));
            msgBodies.add(msgBody);
        }
        body.put("MsgBody",msgBodies);

        this.timService.post(api, body);
    }



    @Override
    public ChatVo batchSendTextMsg(@NonNull String fromAccount, @NonNull List<String> toAccounts, @NonNull List<String> msgList, ChatMsgEnum chatMsgEnum) throws TimException {
        String api = "v4/openim/batchsendmsg";

        if( chatMsgEnum == null ){
            chatMsgEnum = ChatMsgEnum.NoSync ;
        }

        Map<String, Object> body = new HashMap<>(4);
        body.put("SyncOtherMachine", chatMsgEnum.getType());
        body.put("From_Account",fromAccount);
        body.put("To_Account",toAccounts);
        body.put("MsgRandom", RandomUtils.nextInt(10000000,100000000));

        List<MsgBody> msgBodies = new ArrayList<>();

        for (String msg : msgList) {
            MsgBody msgBody = new MsgBody();
            msgBody.setMsgType("TIMTextElem");
            msgBody.setMsgContent(new MsgContent(msg));
            msgBodies.add(msgBody);
        }
        body.put("MsgBody",msgBodies);

        String jsonResult = this.timService.post(api, body);

        return JsonUtils.fromJson(jsonResult, ChatVo.class);
    }



    @Override
    public void sendCustomMsg(@NonNull String fromAccount, @NonNull String toAccount, @NonNull MsgCustomContent msgCustomContent, ChatMsgEnum chatMsgEnum) throws TimException {
        String api = "v4/openim/sendmsg";

        if( chatMsgEnum == null ){
            chatMsgEnum = ChatMsgEnum.NoSync ;
        }

        Map<String, Object> body = new HashMap<>(4);
        body.put("SyncOtherMachine", chatMsgEnum.getType());
        body.put("From_Account",fromAccount);
        body.put("To_Account",toAccount);
        body.put("MsgRandom", RandomUtils.nextInt(10000000,100000000));

        List<MsgBody> msgBodies = new ArrayList<>();

        MsgBody msgBody = new MsgBody();
        msgBody.setMsgType("TIMCustomElem");
        msgBody.setMsgContent(msgCustomContent);
        msgBodies.add(msgBody);
        body.put("MsgBody",msgBodies);

        this.timService.post(api, body);
    }


    @Override
    public ChatVo batchSendCustomMsg(@NonNull String fromAccount, @NonNull List<String> toAccounts, @NonNull MsgCustomContent msgCustomContent, ChatMsgEnum chatMsgEnum) throws TimException {
        String api = "v4/openim/batchsendmsg";

        if( chatMsgEnum == null ){
            chatMsgEnum = ChatMsgEnum.NoSync ;
        }

        Map<String, Object> body = new HashMap<>(4);
        body.put("SyncOtherMachine", chatMsgEnum.getType());
        body.put("From_Account",fromAccount);
        body.put("To_Account",toAccounts);
        body.put("MsgRandom", RandomUtils.nextInt(10000000,100000000));

        List<MsgBody> msgBodies = new ArrayList<>();

        MsgBody msgBody = new MsgBody();
        msgBody.setMsgType("TIMCustomElem");
        msgBody.setMsgContent(msgCustomContent);

        msgBodies.add(msgBody);

        body.put("MsgBody",msgBodies);

        String jsonResult = this.timService.post(api, body);

        return JsonUtils.fromJson(jsonResult, ChatVo.class);
    }










}
